from random import Random


class Player(list):
    def __init__(self, name=None):
        """ same as monster list """

        # TODO TODO TODO fix possible collision
        pname = 'Player'+str(Random().randint(1111,9999))
        if not name == None:
            pname = name

        # name, x, y, score(points), color, lives(3 by default)
        self.ARRAY_LENGTH = 6 # matches however long the array values are
        self.INDEX_NAME = 0
        self.INDEX_X = 1
        self.INDEX_Y = 2
        self.INDEX_SCORE = 3
        self.INDEX_COLOR = 4
        self.INDEX_LIVES = 5

        self.extend([pname, -1, -1, 0, 0, 3])

    def AddPlayer(self, name=None):
        # TODO TODO TODO fix possible collision
        pname = 'Player'+str(Random().randint(1111,9999))
        if not name == None:
            pname = name

        # name, x, y, points, color, difficulty
        self.extend([pname, -1, -1, 0, 0, 3])

    def FindIndex(self, name):
        ''' internal function that will return an index '''
        try:
            i = self.index(name)
        except ValueError:
            raise ValueError, 'Name not found' #TODO TODO TODO custom error
        return i

    def FindByName(self, name):
        ''' return info about a player '''
        i = self.FindIndex(name)

        # since player name is found, return the splice
        return self[i: (i+self.ARRAY_LENGTH)+1]

    def SetColor(self, name, color):
        ''' set color of player '''
        i = self.FindIndex(name)
        # calculate the index location that we want
        loc = i + self.INDEX_COLOR
        # set the self to that color given
        self[loc] = color

    def SetX(self, name, x):
        ''' set an attribute of player '''
        i = self.FindIndex(name)
        # calculate the index location that we want
        loc = i + self.INDEX_X
        # set the self to that color given
        self[loc] = x
        
    def SetY(self, name, y):
        ''' set an attribute of player '''
        i = self.FindIndex(name)
        # calculate the index location that we want
        loc = i + self.INDEX_Y
        # set the self to that color given
        self[loc] = y

    def AddScore(self, name, score, allow_negative=False):
        ''' adds (or subtracts if given a negative number) to the current player's score
            if allow_negative is TRUE, then the score is allowed to drop below 0
        '''
        i = self.FindIndex(name)
        loc = i + self.INDEX_SCORE
        if (self[loc] + score) < 0 and allow_negative == False:
            self[loc] = 0
            return

        self[loc] += score        

    def AddLives(self, name, lives):
        ''' adds (or subtracts if given a negative number) to the current player's lives
            cannot have negative lives
        '''
        i = self.FindIndex(name)
        loc = i + self.INDEX_LIVES
        if (self[loc] + lives) < 0:
            self[loc] = 0
            return

        self[loc] += lives

    def GetDead(self, name):
        '''Get whether or not the player is dead'''
        i = self.FindIndex(name)
        mylives = self[i + self.INDEX_LIVES]
        if mylives <= 0:
            return True
        else:
            return False

    def GetPos(self, name):
        ''' get the position (x, y) for a player '''
        i = self.FindIndex(name)
        # calculate the index location that we want
        loc1 = i + self.INDEX_X
        loc2 = i + self.INDEX_Y
        # return the list with it
        return [ self[loc1], self[loc2] ]
